package com.study.services;

import java.util.Date;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.study.model.mdao.QaMapper;

@Service
@Transactional(rollbackOn = Exception.class)
public class QaService {
	@Autowired
	QaMapper mapper;
	
	/**
	 * 根据条件查询
	 * @param i
	 * @param j
	 * @param string
	 * @param string2
	 * @return
	 */
	public List<JSONObject> selectBySel(int pageNum, int pageSize, String input, String selects) {
		PageHelper.startPage(pageNum, pageSize);
		return mapper.selectByWhere(selects,"%"+input+"%");
	}
	
	/**
	 * 添加Qa
	 * @param theme
	 * @param anwser
	 * @param type
	 * @param useid 
	 * @return
	 */
	public boolean addService(String theme, String anwser, String type) {
		int row = mapper.save(theme,anwser,type,new Date());
		return row>0?true:false;
	}
	
	/**
	 * 编辑
	 * @param id
	 * @param theme
	 * @param anwser
	 * @param type
	 * @return
	 */
	public boolean editorQa(String id, String theme, String anwser, String type) {
		int row = mapper.editor(id,theme,anwser,type);
		return row>0?true:false;
	}
	
	/**
	 * 根据id查询
	 * @param parameter
	 * @return
	 */
	public JSONObject selectBycid(String parameter) {
		
		return mapper.selectById(parameter);
	}
	
	/**
	 * 根据id删除
	 * @param id
	 * @return
	 */
	public boolean delete(Integer id) {
		int row = mapper.delete(id);
		return row>0?true:false;
	}
	/**
	 * 模糊查询Qa
	 * @param msg
	 * @return
	 */
	public List<JSONObject> findByLike(String msg) {
		if (!"".equals(msg) &&  msg != null) {
			return  mapper.selectByLike("%"+msg+"%");
		}else {
			return null;
		}
	}
	
	/**
	 * 添加次数
	 * @param qaid
	 * @return
	 */
	public boolean addQaCount(String qaid) {
		int row = mapper.addQaCount(qaid);
		return row>0?true:false;
	}

}
